<?php
/**
 * CreateTime: 2023/7/25 15:23
 */

namespace App\Func\ScopeFunc;

use App\Func\TokenFuc;
use App\Models\Department\DepartmentModel;
use App\Models\Staff\StaffDepartmentModel;
use App\Models\Staff\StaffModel;

class AuthScopeStaffFunc
{
    public static function getDepartmentNextDeptIds(){
        $staffInfo = TokenFuc::getCurrentStaffInfo();
        // 获取当前人所在部门
        $departModel = new DepartmentModel();
        $departmentIds = $departModel->select(['department_id'])
            ->where('department_level','like',".".$staffInfo['department_id'].".%")
            ->get()->toArray();
        if($departmentIds){
            $departmentIdsFmt = array_column($departmentIds,'department_id');
        }
        $departmentIdsFmt[] = $staffInfo['department_id'];
        return $departmentIdsFmt;
    }


    public static function getDepartmentStaffByCurrentStaffId(){
        $staffInfo = TokenFuc::getCurrentStaffInfo();
        // 获取当前人所在的公司
        $companyId = $staffInfo['company_id'];
        // 获取当前人所在部门
        $departModel = new DepartmentModel();
        // 获取当前公司下所有员工信息
        $staffModel = new StaffModel();
        $staffDepartmentModel = new StaffDepartmentModel();
        // 获取当前用户的全部部门
        $departmentList = $staffDepartmentModel->select([
            'department_id'
        ])->where('staff_id',$staffInfo['staff_id'])->get()->toArray();
        // 获取当前部门的所有下级部门
        $departmentParentChildIds = array_column($departmentList,'department_id');
        foreach($departmentParentChildIds as $value){
            $ids = $departModel->select('department_id')->where('department_level','LIKE',"%".$value.".%")->get()->toArray();
            if($ids){
                $departmentParentChildIds = array_merge($departmentParentChildIds,array_column($ids,'department_id'));
            }
        }
        // 获取当前部门下的所有员工id
        return $staffDepartmentModel->select(['staff_id'])->whereIn('department_id',$departmentParentChildIds)->get();
    }
}